#pragma once

// 文件版本

#include <iostream>
#include <string>
#include <unordered_map>
#include <cassert>
#include <fstream>

#include "../commen/log.hpp"
#include "../commen/util.hpp"

// 根据questions.list文件,加载所有的题目信息到内存里面
// oj_model:主要用来和数据(题目)进行交互,对外提供访问数据(题目)的接口

namespace ns_model
{
    struct question
    {
        int number;             // 题目编号,是唯一的
        std::string title;      // 题目的标题
        std::string difficulty; // 题目的难度
        int cpu_limit;          // 题目的时间要求(单位是S)
        int mem_limit;          // 题目的空间要求(单位是Kb)
        std::string desc;       // 题目的具体描述
        std::string header;     // 题目预设给用户在线编辑器的代码
        std::string tail;       // 题目的测试用例,需要和header进行拼接,形成完整的代码
    };

    const std::string questions_list_path = "./questions/questions.list";
    const std::string questions_path = "./questions/";

    class model
    {
    public:
        model() : _question()
        {
            assert(load_questions_list(questions_list_path));
        }

        ~model() {}

        bool load_questions_list(const std::string &questions_list_path)
        {
            // 加载配置文件:questions.list + 题目编号对应的题目信息
            std::ifstream in(questions_list_path);
            if (!in.is_open())
            {
                ns_log::LOG(FATAL) << "加载题库失败,请检查是否存在题库文件!" << std::endl;
                return false;
            }

            std::string line;
            while (getline(in, line))
            {
                std::vector<std::string> tokens;
                ns_util::string_util::split_string(line, &tokens, " ");
                if (tokens.size() != 5)
                {
                    ns_log::LOG(WARNING) << "该题目切分失败,请检查题库文件!" << std::endl;
                }

                question q;
                q.number = atoi(tokens[0].c_str());
                q.title = tokens[1];
                q.difficulty = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.mem_limit = atoi(tokens[4].c_str());

                std::string number_question_path = questions_path;
                number_question_path += std::to_string(q.number);
                number_question_path += "/";

                ns_util::file_util::read_file(number_question_path + "desc.txt", &(q.desc), true);
                ns_util::file_util::read_file(number_question_path + "header.cpp", &(q.header), true);
                ns_util::file_util::read_file(number_question_path + "tail.cpp", &(q.tail), true);

                _question.insert({q.number, q});
            }

            ns_log::LOG(INFO) << "题目加载成功" << std::endl;
            in.close();
            return true;
        }

        bool get_all_questions(std::vector<question> *all_questions)
        {
            if (_question.size() == 0)
            {
                ns_log::LOG(ERROR) << "用户获取题库失败" << std::endl;
                return false;
            }

            for (const auto &e : _question)
            {
                all_questions->push_back(e.second);
            }

            return true;
        }

        bool get_one_question(int number, question *ques)
        {
            const auto &iter = _question.find(number);
            if (iter == _question.end())
            {
                ns_log::LOG(ERROR) << "用户获取题目失败,题目编号为: " << number << std::endl;
                return false;
            }

            (*ques) = iter->second;
            return true;
        }

    private:
        // 题目编号对应的题目信息
        std::unordered_map<int, question> _question;
    };
}
